﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using BrewXML.Core.Services.NavigationServices;
using BrewXML.Core.ViewModels;
using BrewXML.WindowsPhone.Data;
using Cirrious.CrossCore;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace BrewXML.WindowsPhone
{
    public partial class StyleDetailPage : PhoneApplicationPage
    {
        #region View Model
        public StyleDetailPageViewModel ViewModel
        {
            get
            {
                return this.DataContext as StyleDetailPageViewModel;
            }
        }
        #endregion

        #region OnNavigateTo

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
            base.OnNavigatedTo(e);
            if (ViewModel.EnableAppbar == true)
            {
                this.ApplicationBar.IsVisible = true;
            }

            else
            {
                this.ApplicationBar.IsVisible = false;
            }
            
        }
        #endregion

        #region Build Application Bar
        private void BuildApplicationBar()
        {
            var appBar = new ApplicationBar() { Opacity = 1, Mode = ApplicationBarMode.Default };
            var btnAdd = new ApplicationBarIconButton(new Uri("/Asset/save.png", UriKind.Relative)) { Text = "Save" };
            btnAdd.Click += (sender, args) =>
            {
               
                if (!ViewModel.AllowAdd)
                {
                    Mvx.Resolve<HomePageViewModel>().STYLES.Save("Styles.xml", "STYLES");
                    var ttt = Mvx.Resolve<HomePageViewModel>().STYLES;
                    Mvx.Resolve<HomePageViewModel>().STYLES = null;
                    Mvx.Resolve<HomePageViewModel>().STYLES = ttt;
                }
                else
                {
                    Mvx.Resolve<HomePageViewModel>().STYLES.Add(ViewModel.Style);
                    Mvx.Resolve<HomePageViewModel>().STYLES.Save("Styles.xml", "STYLES");
                    var ttt = Mvx.Resolve<HomePageViewModel>().STYLES;
                    Mvx.Resolve<HomePageViewModel>().STYLES = null;
                    Mvx.Resolve<HomePageViewModel>().STYLES = ttt;
                    ViewModel.AllowAdd = false;
                }
            };
            appBar.Buttons.Add(btnAdd);

            var btndelete = new ApplicationBarIconButton(new Uri("/Asset/delete.png", UriKind.Relative)) { Text = "Delete" };
            btndelete.Click += (sender, args) =>
            {
                int index = Mvx.Resolve<HomePageViewModel>().STYLES.IndexOf(ViewModel.Style);
                Mvx.Resolve<HomePageViewModel>().STYLES.RemoveAt(index);
                Mvx.Resolve<HomePageViewModel>().STYLES.Save("Styles.xml", "STYLES");
                var ttt = Mvx.Resolve<HomePageViewModel>().STYLES;
                Mvx.Resolve<HomePageViewModel>().STYLES = null;
                Mvx.Resolve<HomePageViewModel>().STYLES = ttt;
                Mvx.Resolve<INavigationService>().GoBack();
            };
            appBar.Buttons.Add(btndelete);
            this.ApplicationBar = appBar;
            
        }
        #endregion
        public StyleDetailPage()
        {
            InitializeComponent();
            BuildApplicationBar();
        }

        private void Color_OnLostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            if (!textBox.Text.Contains("srm"))
                textBox.Text = string.Format("{0} srm", textBox.Text);
        }
    }
}